define(["moment"], function (moment) {
    return ["$scope", "$rootScope","$filter", "$stateParams", "config", "MMWService", "$calculater",
    function ($scope,$rootScope, $filter, $stateParams, config, $MMWService, $calculater) {
        $scope.init = function(){
            $scope.workNo_info={
                wo_no:"",
                prdct_no:"",
                prdct_name:"",
                prdct_spec:""
            };
            $scope.reelNo_info={
                reel_no:'',
                mnf:'',
                material_no:'',
                actual_qty:''
            };
            $scope.chip_detail = {
                burn_qty:'',
                new_material_no:'',
                ic_format:'',
                new_reel_no:'',
                burn_no:'',
                burn_value:''
            }
            $scope.is_print = '0';
            $scope.wo_no = '';
            $scope.reel_no = '';
        };

        $scope.back = function(){
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('home', true);
        };

        $scope.barcodeScan = function(event){
            if(event.keyCode == 13){
                $scope.search();
            }
        };

        $scope.confirm = function(){
            if($scope.chip_detail.burn_qty==''){
                $rootScope.showAlert($filter('translate')('SMT2701.msg.burn_qty_havent_written'));
            }else{
                if((+$scope.chip_detail.burn_qty) < (+$scope.reelNo_info.actual_qty) && $scope.chip_detail.new_reel_no == ''){
                    $rootScope.showAlert($filter('translate')('SMT2701.msg.must_input_nw_reel_no'));
                }else{
                    let content = {
                        ip:config.server.ip,
                        port:config.server.port,
                        print_list:[{
                            burn_qty:$scope.chip_detail.burn_qty,
                            reel_qty:$scope.reelNo_info.actual_qty,
                            reel_no:$scope.reelNo_info.reel_no,
                            new_reel_no:$scope.chip_detail.new_reel_no,
                            material_no:$scope.reelNo_info.material_no,
                            new_material_no:$scope.chip_detail.new_material_no,
                            mo_no:$scope.workNo_info.wo_no,
                            product_ver:'',
                            product_no:$scope.workNo_info.prdct_no,
                            burn_no:$scope.chip_detail.burn_no,
                            mnf:$scope.reelNo_info.mnf,
                            burn_value:$scope.chip_detail.burn_value,
                            ic_format:$scope.chip_detail.ic_format,
                        }],
                        is_print:$scope.is_print,
                        is_reprint:'0'
                    }
                    $MMWService.sendToServer({
                        uri: 'ServicesSMT.Module_SMT.ic_save_print_process',
                        content,
                        success : function(data){
                            if($scope.is_print=='1'){//如果is_print是1，跳列印
                                printTag(data.ic_print_list,0);
                            }else if($scope.is_print=='0'){//如果is_print是0，跳存檔成功
                                $rootScope.showToast($filter('translate')('SMT2701.msg.save_success'));
                            }
                        }
                    }); 
                }
            }
        };

        $scope.print_or_not=function(){
            if($scope.is_print == '1'){
                $scope.is_print = '0'
            }else if($scope.is_print = '0'){
                $scope.is_print = '1'
            }
        }

        //更改燒錄數
        $scope.input_burn_qty = function(event){
            var target = event.currentTarget;
            angular.element(target).addClass('is-focus');
            $calculater.open(function(num, dialog){
                angular.element(target).removeClass('is-focus');
                if($scope.reelNo_info.actual_qty >= (+num)){
                    $scope.chip_detail.burn_qty = num;
                    dialog.hide(); 
                }else{
                    $rootScope.showAlert($filter('translate')('SMT2701.msg.burn_qty_too_much'));
                }
            },
            function(dialog){
                angular.element(target).removeClass('is-focus');
                dialog.hide();
            },
            {
                defaultNum: $scope.chip_detail.burn_qty,
                decimal: false
            });
        };


        $scope.loadWoNoList= function(){
            $MMWService.showQueryListPNIG({
                page_mode: 1,
                pageRecord:50,
                condition: [
                    {name:$filter('translate')('common.queryList.wo_c'), column_name:'MONo', query_mode:'2', value:$scope.wo_no}
                ],
                header: [
                    {label:$filter('translate')('common.queryList.wo_c'), code:'page_no'},
                    {label:$filter('translate')('common.queryList.item_c'), code:'productno'},
                    {label:$filter('translate')('common.queryList.item_n'), code:'productname'},
                    {label:$filter('translate')('common.queryList.wo_no_qty'), code:'moqty', width:'110px'}
                ],
                setCondition:function(conditions){
                    conditions.push({column_name:'MOSTATE', query_mode:'0', value:'3', merge_condition_model:'AND' });
                    return conditions;
                },
                selectCode:$scope.workNo_info.wo_no,
                confirm : function(selectItem){
                    $scope.workNo_info.wo_no = selectItem.page_no;
                    $scope.workNo_info.prdct_no = selectItem.productno;
                    $scope.workNo_info.prdct_name = selectItem.productname;
                    $scope.workNo_info.moqty = selectItem.moqty;
                    $scope.workNo_info.prdct_spec = selectItem.prdct_spec;
                }
            });
        }

        $scope.loadReelNoList = function(){
            if($scope.workNo_info.wo_no ==''){

            }else{
                $MMWService.showQueryListPNIG({
                    page_mode: 'smt_ic_reel_info_get',
                    pageRecord:50,
                    key:'page_no',
                    condition: [
                        {name:$filter('translate')('SMT2701.dialog_2.reel_no'), column_name:'ReelNo', query_mode:'2', value:$scope.reelNo_info.reel_no}
                    ],
                    header: [
                        { label: $filter('translate')('SMT2701.dialog_2.reel_no'), code: 'reel_no' },
                        { label: $filter('translate')('SMT2701.dialog_2.mtrl_no'), code: 'material_no' },
                        { label: $filter('translate')('SMT2701.dialog_2.mnf'), code: 'mnf' },
                        { label: $filter('translate')('SMT2701.dialog_2.reel_num'), code: 'actual_qty' }
                    ],
                    setCondition:function(conditions){
                        conditions.push({column_name:'ResourceState', query_mode:'0', value:'0', merge_condition_model:'AND' });
                        return conditions;
                    },
                    selectCode:$scope.reelNo_info.reel_no,
                    confirm : function(selectItem){
                        $scope.reelNo_info = selectItem;
                    }
                });
            }
        }

        
        $scope.loadNewMtrlNoList = function(){
            $MMWService.showQueryListPNIG({
                page_mode: 'smt_material_info_get',
                pageRecord:50,
                key:'page_no',
                condition: [
                    {name:$filter('translate')('SMT2701.dialog_3.new_reel_no'), column_name:'MaterialNo', query_mode:'2', value:$scope.chip_detail.new_material_no}
                ],
                header: [
                    { label: $filter('translate')('SMT2701.dialog_3.new_reel_no'), code: 'material_no' },
                    { label: $filter('translate')('SMT2701.dialog_3.new_mtrl_name'), code: 'material_name' },
                    { label: $filter('translate')('SMT2701.dialog_3.new_mtrl_spec'), code: 'material_spec' },
                ],
                setCondition:function(conditions){
                    conditions.push({column_name:'ISSUESTATE', query_mode:'0', value:'2', merge_condition_model:'AND' });
                    return conditions;
                },
                selectCode:$scope.chip_detail.new_material_no,
                confirm : function(selectItem){
                    $scope.chip_detail.new_material_no = selectItem.material_no;
                }
            });
        }

        function printTag(tags, index){
            if(tags && index < tags.length){
                if(window.nodeRequire){
                    var exec = window.nodeRequire('child_process').exec;
                    $rootScope.showToast($filter('translate')('SMT18.msg.prpare_print', {index:index}));
                    exec(tags[index].commandline, function(error, stdout, stderr){
                        if(error){
                            $rootScope.showAlert($filter('translate')('E01.msg.cantprint'));
                        }
                        index = index +1;
                        printTag(tags, index);
                    });
                }
            }
        };

    }
];});
